package cn.gene.timeyan.article.webapi.job;

import cn.gene.timeyan.article.webapi.mapper.CommentMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
public class CommentJob implements Job {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Autowired
    private CommentMapper commentMapper;
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        Set<String> set=redisTemplate.keys("articleDetails:*:commentDetail:*");
        for (String item:set) {
            Long commentId=Long.parseLong(item.substring(item.lastIndexOf(":")+1));
            //获取回复数
            int reply=(int)redisTemplate.opsForHash().get(item,"replys");
            int line=commentMapper.updateReplyNumByCommentId(commentId,reply);
            redisTemplate.delete(item);
        }
    }
}
